#include"ClientUtils.h"
ClientUtils::ClientUtils(/* args */)
{
}

ClientUtils::~ClientUtils()
{
}

uint32_t ClientUtils::getLocalHost2Uint32()
{
    struct addrinfo hints, *res;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_INET; // IPv4
    hints.ai_socktype = SOCK_STREAM;

    int status = getaddrinfo("localhost", NULL, &hints, &res);
    if (status != 0) {
        std::cout << "获取 IP 地址失败: " << gai_strerror(status) << std::endl;
        return 1;
    }

    struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
    uint32_t ip = ipv4->sin_addr.s_addr;


    freeaddrinfo(res);
    return ip;
}

string ClientUtils::Uint322Host(uint32_t ipv4)
{
    struct in_addr addr;
    addr.s_addr = ipv4; // 将主机字节序转换为网络字节序
    const char* ipStr = inet_ntoa(addr); // 使用库函数将整数转为点分十进制字符串
    return std::string(ipStr);
}
